const app = getApp()
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    eIndex: 1,
    eIndex2: 1,
    selectSendStore:false,
    selectSendAddress:false,
    selectReceiveStore: false,
    selectReceiveAddress: false
  },

  // 切换类名on(租车)
  tab: function (e) {
    this.setData({
      eIndex: e.currentTarget.dataset.index
    })
  },

  // 切换类名on（还车）
  tab2: function (e) {
    this.setData({
      eIndex2: e.currentTarget.dataset.index,
      bool2: true
    })
  },

  // 跳转到地址
  toAddr: function (e) {
    var flag = e.currentTarget.dataset.value
    wx.redirectTo({
      url: '../address/address?startTime=' + this.data.startTime + '&endTime=' + this.data.endTime + '&day=' + this.data.day + '&startHour=' + this.data.startHour + '&endHour=' + this.data.endHour + '&productId=' + this.data.productId + '&startCityId=' + this.data.startCityId + '&endCityId=' + this.data.endCityId + '&flag=' + flag + '&startCityName=' + this.data.startCityName + '&endCityName=' + this.data.endCityName +'&sendType=' + this.data.sendType + '&sendName=' + this.data.sendName + '&sendMobile=' + this.data.sendMobile + '&sendAddress=' + this.data.sendAddress + '&receiveType=' + this.data.receiveType + '&receiveName=' + this.data.receiveName + '&receiveMobile=' + this.data.receiveMobile + '&receiveAddress=' + this.data.receiveAddress + '&type=' + this.data.type
    })
  },

  // 跳转到我的地址
  toUserAddr: function (e) {
   
    var flag = e.currentTarget.dataset.value
    wx.redirectTo({
      url: '../user-address/user-address?startTime=' + this.data.startTime + '&endTime=' + this.data.endTime + '&day=' + this.data.day + '&startHour=' + this.data.startHour + '&endHour=' + this.data.endHour + '&productId=' + this.data.productId + '&startCityId=' + this.data.startCityId + '&endCityId=' + this.data.endCityId + '&flag=' + flag + '&startCityName=' + this.data.startCityName + '&endCityName=' + this.data.endCityName + '&sendType=' + this.data.sendType + '&sendName=' + this.data.sendName + '&sendMobile=' + this.data.sendMobile + '&sendAddress=' + this.data.sendAddress + '&receiveType=' + this.data.receiveType + '&receiveName=' + this.data.receiveName + '&receiveMobile=' + this.data.receiveMobile + '&receiveAddress=' + this.data.receiveAddress+'&type='+this.data.type
    })
  },

  // 跳转到门店
  toStore: function (e) {
    var flag = e.currentTarget.dataset.value
    wx.redirectTo({
      url: '../store/store?startTime=' + this.data.startTime + '&endTime=' + this.data.endTime + '&day=' + this.data.day + '&startHour=' + this.data.startHour + '&endHour=' + this.data.endHour + '&productId=' + this.data.productId + '&startCityId=' + this.data.startCityId + '&endCityId=' + this.data.endCityId + '&flag=' + flag + '&startCityName=' + this.data.startCityName + '&endCityName=' + this.data.endCityName + '&sendType=' + this.data.sendType + '&sendName=' + this.data.sendName + '&sendMobile=' + this.data.sendMobile + '&sendAddress=' + this.data.sendAddress + '&receiveType=' + this.data.receiveType + '&receiveName=' + this.data.receiveName + '&receiveMobile=' + this.data.receiveMobile + '&receiveAddress=' + this.data.receiveAddress+'&type='+this.data.type
    })
  },

  // 跳转到验证身份证
  toUpId: function () {
    console.log('productId=' + this.data.productId)
    console.log('startTime=' + this.data.startTime)
    console.log('startHour=' + this.data.startHour)
    console.log('endTime=' + this.data.endTime)
    console.log('endHour=' + this.data.endHour)
    console.log('day=' + this.data.day)
    console.log('startCityId=' + this.data.startCityId)
    console.log('startCityName=' + this.data.startCityName)
    console.log('endCityId=' + this.data.endCityId)
    console.log('endCityName=' + this.data.endCityName)
    console.log('sendType=' + this.data.sendType)
    console.log('sendName=' + this.data.sendName)
    console.log('sendMobile=' + this.data.sendMobile)
    console.log('sendAddress=' + this.data.sendAddress)
    console.log('receiveType=' + this.data.receiveType)
    console.log('receiveName=' + this.data.receiveName)
    console.log('receiveMobile=' + this.data.receiveMobile)
    console.log('receiveAddress=' + this.data.receiveAddress)
    console.log("type=" + this.data.type)
    if (util.isBlank(this.data.sendType)){
      wx.showToast({
        title: '请选择送车地址或门店',
        icon: 'none',
        duration: 1500
      })
      return
    }
    if (util.isBlank(this.data.receiveType)) {
      wx.showToast({
        title: '请选择还车地址或门店',
        icon: 'none',
        duration: 1500
      })
      return
    }


    let that = this
      wx.request({
        url: 'https://small.qiongyazuche.com/sunday/small/order/createOrder',
        method: "POST",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          "type": this.data.type,
          "memberId": app.globalData.userInfo.id,
          "productId": this.data.productId,
          "day": this.data.day,
          "startTime": this.data.startTime,
          "startHour":this.data.startHour,
          "endTime": this.data.endTime,
          "endHour":this.data.endHour,
          "sendType": this.data.sendType,
          "sendName": this.data.sendName,
          "sendMobile": this.data.sendMobile,
          "sendAddress": this.data.sendAddress,
          "receiveType": this.data.receiveType,
          "receiveName": this.data.receiveName,
          "receiveMobile": this.data.receiveMobile,
          "receiveAddress": this.data.receiveAddress
        },
        success: function (res) {


          if (res.data.code != 0) {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 1500
            })
          } else {
              if (that.data.userInfo.userType != 2){
                wx.navigateTo({
                  url: '../up-drive/up-drive'
                })
            }else{
              wx.navigateTo({
                url: '../order-details/order-details?orderId=' + res.data.result.orderIds
              })
            }
          }
        }
      })
  
  
    
  },
 

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      productId: options.productId,
      couponValue: app.globalData.couponValue,
      type:options.type,
      startTime: options.startTime,
      startHour: options.startHour,
      endTime: options.endTime,
      endHour: options.endHour,
      day: options.day,
      startCityId: options.startCityId,
      startCityName: options.startCityName,
      endCityId: options.endCityId,
      endCityName:options.endCityName,
      sendType: options.sendType ? options.sendType:'',
      sendName: options.sendName ? options.sendName : '',
      sendMobile: options.sendMobile ? options.sendMobile : '',
      sendAddress: options.sendAddress ? options.sendAddress : '',
      receiveType: options.receiveType ? options.receiveType : '',
      receiveName: options.receiveName ? options.receiveName : '',
      receiveMobile: options.receiveMobile ? options.receiveMobile : '',
      receiveAddress: options.receiveAddress ? options.receiveAddress : ''
    })
    console.log('productId=' + this.data.productId)
    console.log('startTime=' + this.data.startTime)
    console.log('startHour=' + this.data.startHour)
    console.log('endTime=' + this.data.endTime)
    console.log('endHour=' + this.data.endHour)
    console.log('day=' + this.data.day)
    console.log('startCityId=' + this.data.startCityId)
    console.log('startCityName=' + this.data.startCityName)
    console.log('endCityId=' + this.data.endCityId)
    console.log('endCityName=' + this.data.endCityName)
    console.log('sendType=' + this.data.sendType)
    console.log('sendName=' + this.data.sendName)
    console.log('sendMobile=' + this.data.sendMobile)
    console.log('sendAddress=' + this.data.sendAddress)
    console.log('receiveType=' + this.data.receiveType)
    console.log('receiveName=' + this.data.receiveName)
    console.log('receiveMobile=' + this.data.receiveMobile)
    console.log('receiveAddress=' + this.data.receiveAddress)
    if(this.data.sendType==1){
        this.setData({
          eIndex:0,
          eIndex:0
        })
    }
    if (this.data.receiveType == 1){
      this.setData({
        eIndex2: 0,
        eIndex2: 0
      })
    }
    if (this.data.sendType == 2) {
      this.setData({
        eIndex: 1,
        eIndex: 1
      })
    }
    if (this.data.receiveType == 2) {
      this.setData({
        eIndex2: 1,
        eIndex2: 1
      })
    }
    this.loadProduct()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.sendType==2){
      this.setData({
        selectSendStore:true
      })
    }
    if (this.data.receiveType==2){
      this.setData({
        selectReceiveStore: true
      })
    }

    this.setUserInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '琼崖租车',
      path: '/pages/index/index'
    }
  },
  loadProduct: function () {
    let that = this
      if(this.data.type==1){
        wx.request({
          url: 'https://small.qiongyazuche.com/sunday/small/product/findOne',
          method: 'POST',
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          data: {
            'productId': that.data.productId
          },
          success: function (res) {
            that.setData({
              product: res.data.result,
              totalPrice: res.data.result.price.toFixed(2)
            })
          }
        })
      }else{

        wx.request({
          url: 'https://small.qiongyazuche.com/sunday/small/meal/getDetail',
          method: 'POST',
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          data: {
            'id': that.data.productId
          },
          success: function (res) {
            that.setData({
              product: res.data.result,
              totalPrice: res.data.result.price.toFixed(2)
            })
          }
        })
      }

  },
  setUserInfo :function(){
    let that = this
    wx.request({
      url: 'https://small.qiongyazuche.com/sunday/small/member/findOne',
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        "memberId": app.globalData.userInfo.id
      },
      success: function (res) {
        that.setData({
          userInfo: res.data.result
        })
        app.globalData.userInfo = res.data.result
      }
    })
  }
})